package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"time"
	"xorm.io/xorm"
)



func main() {
	// build insert datasource
	type User struct{
		Id int64
		Name string
		Age int
		Passwd string `xorm:"varchar(200)"`
		Created time.Time `xorm:"created"`
		Updated time.Time `xorm:"updated"`
	}


	// 数据库连接基本信息
	var (
		userName  string = "root"
		password  string = "123456"
		ipAddress string = "127.0.0.12.9"
		port      int    = 3306
		dbName    string = "go_project"
		charset   string = "utf8mb4"
	)
	// 构建数据库连接信息
	dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s", userName, password, ipAddress, port, dbName, charset)

	engine, err := xorm.NewEngine("mysql",dataSourceName)
	if err !=nil {
		fmt.Println("mysql connect fair!")
	}

	// engine.Sync
	err = engine.Sync(new(User))
	if err != nil{
		fmt.Println("tables sync fair!");
	}



	// engine.Insert() 插入 对象， 返回值， 受影响的行数
	user := User{Id:1000,Name: "alen",Age: 18,Passwd: "123456"}
	n, err := engine.Insert(&user)
	if n >  1 {
		fmt.Println("数据插入成功: ",n)
	}

	user1 := User{Id:1001,Name: "wusir",Age: 89,Passwd: "123456"}
	user2 := User{Id:1002,Name: "tom",Age: 36,Passwd: "123456"}
	n,_ = engine.Insert(&user1,&user2)
	fmt.Println(n)
	if n >= 1{
		fmt.Println("数据插入成功！")
	}

	var users []User
	users = append(users, User{Id:1003,Name: "nexora",Age: 99,Passwd: "123456"})
	users = append(users, User{Id:1004,Name: "sync",Age: 93,Passwd: "123456"})

	n , _ = engine.Insert(&users)

}
